package com.gxa.weixing.pc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.Menu;
import com.gxa.weixing.core.pojo.Role;
import com.gxa.weixing.core.pojo.RoleMenu;
import com.gxa.weixing.core.service.impl.BaseServiceImpl;
import com.gxa.weixing.pc.mapper.RoleMapper;
import com.gxa.weixing.pc.mapper.RoleMenuMapper;
import com.gxa.weixing.pc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.lang.model.element.VariableElement;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleServiceImpl
 * @Author: ws
 * @date 2022/1/6 15:51
 */
@Service("roleService")
@Transactional(rollbackFor = Throwable.class)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Override
    public PageInfo<Role> getAll(Integer page, Integer size) {
        // 开启分页
        PageHelper.startPage(page, size);
        // 条件
        Example example = new Example(Role.class);
        example.orderBy("roleUpdateTime").desc();
        List<Role> roles = roleMapper.selectByExample(example);
        // 返回给前端
        return PageInfo.of(roles);
    }

    @Override
    public List<Menu> getMenus(Long roleId) throws BusinessException {
        if (StringUtils.isEmpty(roleId)) {
            throw new BusinessException(ExceptionCode.ID_NOTNULL);
        }
        List<Menu> menus = roleMapper.getMenus(roleId);
        return menus;
    }

    @Override
    public List<Menu> getMenus() {
        List<Menu> menu = roleMapper.getMenu();
        return menu;
    }

    @Override
    public Integer authorization(Long roleId, Long[] menuIds) throws BusinessException {
        // 删除该角色已有的权限
        Example example = new Example(RoleMenu.class);
        example.createCriteria().andEqualTo("roleId", roleId);
        int i = roleMenuMapper.deleteByExample(example);

        // 如果删除失败，抛出异常
        if (i <= 0) {
            throw new BusinessException(ExceptionCode.OPERATION_EXCEPTION);
        }

        // 反之删除成功，表示有该角色组，添加新的权限
        // 操作影响的行数
        int updates = 0;
        for (Long menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            updates += roleMenuMapper.insertSelective(roleMenu);
        }
        return updates;
    }

    @Override
    public Integer delete(Long id) throws BusinessException {
        // 删除角色信息
        int i = roleMapper.deleteByPrimaryKey(id);
        // 如果角色被删除,同时删除角色拥有的权限
        if (i == 1) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(id);
            int delete = roleMenuMapper.delete(roleMenu);
        }
        return i;
    }

    @Override
    public List<Map> getRoleListWithPermissions() {
        return roleMapper.getRoleListWithPermissions();
    }
}
